﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Services.Description;
using Web.Data;

namespace Web
{
    public partial class Login : System.Web.UI.Page
    {
        SqlConnection myConnection = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            //myConnection = new SqlConnection("user id=PhoenixHD;" +
            //                            "password=142910;server=localhost;" +
            //                            "Trusted_Connection=yes;" +
            //                            "database=db_paving; " +
            //                            "connection timeout=30");
            
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            try
            {

                string username = Request.Form["txtUsername"].ToString();
                string password = Request.Form["txtPassword"].ToString();


                using (var db = new PavingSystemDataContext())
                {
                    var user = db.tbl_users.SingleOrDefault(u => u.user_login_name.Equals(username) && u.user_password.Equals(password));
                    if (user != null)
                    {
                        Response.Redirect("Dashboard.aspx");
                    }
                    else
                    {
                        Response.Redirect("Login.aspx");
                    }
                }
                //using (var db = new PavingSystemDataContext()){

                //    var user = db.tbl_users.SingleOrDefault(u => u.user_login_name.Equals(username) && u.user_password.Equals(password));

                //    if (user != null)
                //    {
                //        Response.Redirect("Dashboard.aspx");
                //    }
                //    else {
                //        Response.Redirect("Login.aspx");
                //    }

                //}


                
                //SqlCommand myCommand = null;
                //SqlDataReader myReader = null;
                //myConnection.Open();

                //myCommand = new SqlCommand("select * from tbl_user",
                //                                         myConnection);
                //myReader = myCommand.ExecuteReader();
                //while (myReader.Read())
                //{
                //    if (username == myReader["user_login_name"].ToString().Trim() && password == myReader["user_password"].ToString().Trim())
                //    {
                //        Response.Redirect("Dashboard.aspx");
                //    }
                //    else
                //    {
                //        Response.Redirect("Login.aspx");
                //    }
                //}
                //myConnection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}